package com.yzlwb.web.controller.skt;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.JsonNode;
import com.yzlwb.common.core.domain.entity.SysDept;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yzlwb.common.annotation.Log;
import com.yzlwb.common.core.controller.BaseController;
import com.yzlwb.common.core.domain.AjaxResult;
import com.yzlwb.common.enums.BusinessType;
import com.yzlwb.skt.domain.SktProject;
import com.yzlwb.skt.service.ISktProjectService;
import com.yzlwb.common.utils.poi.ExcelUtil;
import com.yzlwb.common.core.page.TableDataInfo;

/**
 * 司空项目信息
 * 
 * @author wfy
 * @date 2025-11-06
 */
@RestController
@RequestMapping("/skt/project")
public class SktProjectController extends BaseController
{
    @Autowired
    private ISktProjectService sktProjectService;

    /**
     * 分页查询本地存储的司空项目列表
     */
    @PreAuthorize("@ss.hasPermi('system:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(SktProject sktProject)
    {
        startPage();
        List<SktProject> list = sktProjectService.selectSktProjectList(sktProject);
        return getDataTable(list);
    }

    /**
     * 根据司空二的连接密钥获取项目列表
     * @param twoUserToken 连接密钥
     * @return
     */
    @PostMapping(value = "/getSkProjectInfo")
    public AjaxResult getSkProjectInfo(@RequestBody JsonNode jsonNode){
        String twoUserToken = jsonNode.get("twoUserToken").asText();
        return success(sktProjectService.getSkProjectInfoList(twoUserToken));
    }

    /**
     * 保存_系统项目关联司空二项目
     * @param twoUserToken 连接密钥
     * @param twoName 司空项目名称，多个用@符号隔开
     * @return
     */
    @PostMapping(value = "/saveDeptAssociationSkProject")
    public AjaxResult saveDeptAssociationSkProject(@RequestBody JsonNode jsonNode){
        Long deptId = jsonNode.get("deptId").asLong();
        String twoUserToken = jsonNode.get("twoUserToken").asText();
        String twoNames = jsonNode.get("twoNames").asText();
        return success(sktProjectService.deptAssociationSkProject(deptId,twoUserToken,twoNames));
    }

    /**
     * 同步系统项目已经关联的司空二项目信息
     */
    @GetMapping(value = "/updateDeptByTwoUserTokenAndTwoName")
    public AjaxResult updateDeptByTwoUserTokenAndTwoName()
    {
        return success(sktProjectService.updateDeptByTwoUserTokenAndTwoName(getDeptId()));
    }

    /**
     * 获取所在司空项目下的实时设备列表
     * @return
     */
    @GetMapping(value="/skDevice")
    public AjaxResult getSkDevice(String twoProjectUuid)
    {
        return success(sktProjectService.getSkDevice(getLoginUser().getUser().getDept().getTwoUserToken(),twoProjectUuid));
    }

    /**
     * 获取系统项目下的历史设备列表
     * @return
     */
    @GetMapping(value="/skDeviceHistory")
    public AjaxResult getSkDeviceHistory()
    {
        return success(sktProjectService.getSkDevice(getLoginUser().getUser().getDept().getTwoUserToken(),""));
    }

    /**
     * 导出司空项目列表
     */
    @PreAuthorize("@ss.hasPermi('system:project:export')")
    @Log(title = "司空项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SktProject sktProject)
    {
        List<SktProject> list = sktProjectService.selectSktProjectList(sktProject);
        ExcelUtil<SktProject> util = new ExcelUtil<SktProject>(SktProject.class);
        util.exportExcel(response, list, "司空项目数据");
    }

    /**
     * 获取本地的司空项目详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sktProjectService.selectSktProjectById(id));
    }

    /**
     * 新增司空项目
     */
    @PreAuthorize("@ss.hasPermi('system:project:add')")
    @Log(title = "司空项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SktProject sktProject)
    {
        return toAjax(sktProjectService.insertSktProject(sktProject));
    }

    /**
     * 修改司空项目
     */
    @PreAuthorize("@ss.hasPermi('system:project:edit')")
    @Log(title = "司空项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SktProject sktProject)
    {
        return toAjax(sktProjectService.updateSktProject(sktProject));
    }

    /**
     * 删除司空项目
     */
    @PreAuthorize("@ss.hasPermi('system:project:remove')")
    @Log(title = "司空项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sktProjectService.deleteSktProjectByIds(ids));
    }
}
